import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定ip和端口
s.bind(('192.168.130.32', 8000))
# 指定监听数量
s.listen(128)

while True:
    print('开始。。。。。')
    # conn和客户端建立连接对象，addr是客户端地址
    conn, addr = s.accept()
    # print(conn)
    # print(addr)
    # 获取客户端传递的数据
    data = conn.recv(1024).decode()
    # print(data)
    # 路由匹配
    data_split = data.split(' ')
    print(data_split)
    path = data_split[1]
    if path == '/next':
        response_data = 'HTTP/1.1 200 ok\r\nContent-Type:text/html;charset=utf-8\r\nServer:Ming1.1\r\n\r\n'
        # 返回前端页面
        with open('./templates/next.html', 'rb') as f:
            file_data = f.read()


    # 数据返回  数据是bytes
    # 根据http协议构造响应报文
    # response_data = 'ok'.encode()
    else:
        response_data = 'HTTP/1.1 200 ok\r\nContent-Type:text/html;charset=utf-8\r\nServer:Ming1.1\r\n\r\n'
        # 返回前端页面
        with open('./templates/index.html', 'rb') as f:
            file_data = f.read()
    conn.send(response_data.encode() + file_data)
    # 关闭连接
    conn.close()
    print(('退出。。。。。。'))
